package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.list.model.SearchAttr;
import com.atguigu.gmall.list.model.SearchParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * title:
 * author: Gyh
 * date:2023-05-03 0:24
 * Description:
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 渲染搜索业务
     * @param searchParam
     * @param model
     * @return
     */
    @GetMapping("/list.html")
    public String listHtml(SearchParam searchParam, Model model) {
        //用户提交请求参数 ${searchParam}
        model.addAttribute("searchParam", searchParam);
        //用户回显地址栏中请求 包含搜索条件完整链接 ${urlParam}
        model.addAttribute("urlParam", this.makeUrlParam(searchParam));
        //回显用户已选中品牌面包屑 ${trademarkParam}
        model.addAttribute("trademarkParam", this.makeTrademarkParam(searchParam));
        //回显用户已选中平台属性 ${propsParamList}
        model.addAttribute("propsParamList", this.makePropsParamList(searchParam));
        //回显页面排序效果 type:排序方式 sort:升降序
        model.addAttribute("orderMap", this.makeOrderMap(searchParam.getOrder()));
        //远程调用搜索微服务获取到业务vo对象
        Result<Map> result = listFeignClient.search(searchParam);
        model.addAllAttributes(result.getData());
        return "/list/index";
    }

    /**
     * 根据用户提交选中平台属性过滤条件,返回用户已选择平台属性过滤条件
     *
     * @param searchParam
     * @return
     */
    private List<SearchAttr> makePropsParamList(SearchParam searchParam) {
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            List<String> propList = Arrays.asList(props);
            List<SearchAttr> searchAttrList = propList.stream().map(attrInfoStr -> {
                String[] split = attrInfoStr.split(":");
                if (split != null && split.length == 3) {
                    SearchAttr searchAttr = new SearchAttr();
                    searchAttr.setAttrId(Long.valueOf(split[0]));
                    searchAttr.setAttrValue(split[1]);
                    searchAttr.setAttrName(split[2]);
                    return searchAttr;
                }
                return null;
            }).collect(Collectors.toList());
            return searchAttrList;
        }
        return null;
    }

    /**
     * 根据用户提交选中品牌参数,返回用户已选品牌字符串
     *
     * @param searchParam
     * @return
     */
    private String makeTrademarkParam(SearchParam searchParam) {
        if (StringUtils.isNotBlank(searchParam.getTrademark())) {
            String[] split = searchParam.getTrademark().split(":");
            if (split != null && split.length == 2) {
                return "品牌: " + split[1];
            }
        }
        return null;
    }

    /**
     * 制作用户已选中所有条件URl请求
     *
     * @param searchParam
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder stringBuilder = new StringBuilder("/list.html?");
        if (StringUtils.isNotBlank(searchParam.getKeyword())) {
            stringBuilder.append("&keyword=" + searchParam.getKeyword());
        }
        if (StringUtils.isNotBlank(searchParam.getTrademark())) {
            stringBuilder.append("&trademark=" + searchParam.getTrademark());
        }
        if (searchParam.getCategory1Id() != null) {
            stringBuilder.append("&category1Id=" + searchParam.getCategory1Id());
        }
        if (searchParam.getCategory2Id() != null) {
            stringBuilder.append("&category2Id=" + searchParam.getCategory2Id());
        }
        if (searchParam.getCategory3Id() != null) {
            stringBuilder.append("&category3Id=" + searchParam.getCategory3Id());
        }
        //将用户选中平台属性数组遍历拼接到url地址栏中
        if (searchParam.getProps() != null && searchParam.getProps().length > 0) {
            for (String prop : searchParam.getProps()) {
                String[] split = prop.split(":");
                if (split != null && split.length == 3) {
                    stringBuilder.append("&props=").append(prop);
                }
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 根据用户提交排序方式,回显页面排序效果 type:排序方式 sort:升降序
     *
     * @param order
     * @return
     */
    private Map makeOrderMap(String order) {
        HashMap<String, String> orderMap = new HashMap<>();
        if (StringUtils.isNotBlank(order)) {
            String[] split = order.split(":");
            orderMap.put("type", split[0]);
            orderMap.put("sort", split[1]);
        } else {
            //没有选择排序
            orderMap.put("type", "3");
            orderMap.put("sort", "asc");
        }
        return orderMap;
    }
}
